﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace XCore
{
    public static class DataHelper
    {
        //20111221 bug fixed by tangxj
        /// <summary>
        /// 合并两个DataTable
        /// </summary>
        /// <param name="dtNow">现有Datatable</param>
        /// <param name="dtIN">加入的Datatable</param>
        /// <returns>合并后的datatable\</returns>
        public static DataTable ComBine(DataTable dtNow, DataTable dtIN , string SameColumns)
        {
            ArrayList Names = new ArrayList();
            foreach (DataColumn dc in dtIN.Copy().Columns)
            {
                if (!dtNow.Columns.Contains(dc.Caption))
                {
                    dtNow.Columns.AddRange(new DataColumn[] { new DataColumn(dc.Caption) });
                    Names.Add(dc.Caption);
                }
            }
            for (int i = 0; i < dtNow.Rows.Count; i++)
            {
                string valueNow = dtNow.Rows[i][SameColumns].ToString().ToUpper();
                foreach (DataRow dr in dtIN.Rows)
                {
                    string valueIn = dr[SameColumns].ToString().ToUpper();
                    if (valueIn == valueNow)
                    {
                        foreach (string str in Names.ToArray())
                        {
                            dtNow.Rows[i][str] = dr[str].ToString();
                        }
                    }
                }
            }
            return dtNow;
        }
    }
}
